package su2010.puz.extractors;

import java.awt.image.BufferedImage;

import su2010.puz.Extractor;
import su2010.puz.ImageHelper;
import su2010.puz.util.ImageCls;
import su2010.puz.util.PixelExtractor;

/**
 * ima 0 info gain :(
 * @author Stef
 *
 */
public class StdDeviationMeanExtractor implements Extractor {

	private BufferedImage image;
	private int numOutput = 1;
	private String name = "Standard Deviation of Mean";
	private int color_mode;
	
	public StdDeviationMeanExtractor() {
	}
	
	public StdDeviationMeanExtractor(String name) {
		this.name = name;
	}
	@Override
	public float[] extract() {
		int[][][] pixels = PixelExtractor.getAll(image);
		float mean = 0;
		float sum = 0;
		float[][] meani = new float[3][3];
		int c = pixels.length;
		int r = pixels[0].length;
		for(int i = 0; i<c;i++)
			for(int j =0; j<r;j++){
				mean += pixels[i][j][color_mode];
			}
		mean = mean / (float)(r*c);
//		System.out.println(r+" "+c+" "+mean);
		int ci = c/9;
		int ri = r/9;
//		System.out.println(ri + " "+ci);
		for(int i = 0; i < 3; i++)
			for(int j = 0; j<3;j++){
				for(int k = 0; k < ci; k++)
					for(int l=0;l<ri;l++){
						meani[i][j] += pixels[i*k][j*l][color_mode];
					}
				meani[i][j] /= (float)(ci*ri);
//				System.out.println(meani[i][j]);
				sum += meani[i][j] - mean;
			}
			
		return new float[]{ImageHelper.normalize(sum, -1100, 1100, 0, 1)};
	}

	public void setColor_mode(int color_mode) {
		this.color_mode = color_mode;
	}
	
	@Override
	public int getNumOutput() {
		return numOutput;
	}

	@Override
	public String getName() {
		return ImageHelper.getColorName(color_mode)+" "+name;
	}

	@Override
	public void setImage(ImageCls image) {
		this.image = image.getImage();
	}

}
